﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class GuildModels
    {
        public List<Guild> GetGuild()
        {
            List<Guild> listguild = new List<Guild>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select GuildID,GuildName,Player_Number from TblGuild";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Guild guild = new Guild();
                    guild.GuildID=int.Parse(con.dataRead["GuildID"].ToString());
                    guild.GuildName = con.dataRead["GuildName"].ToString();
                    guild.Player_Number =int.Parse(con.dataRead["Player_Number"].ToString());
                    listguild.Add(guild);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                con.CloseData();
                con = null;
            }
            return listguild;
        }
        public void CreateGuild(Guild guild)
        {
           
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_CreateGuild @GuildName =N'" + guild.GuildName + "', @Player_Number='" + guild.Player_Number + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
 
        }
        public void EditGuild(Guild guild)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_EditGuild @GuildID='" + guild.GuildID + "',  @GuildName =N'" + guild.GuildName + "', @Player_Number='" + guild.Player_Number + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
 
        }
        public void DeleteGuild(int GuildID)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_DeleteGuild @GuildID='"+GuildID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public List<Guild> GetGuildID(int GuildID)
        {
            List<Guild> listguildid = new List<Guild>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_GetGuildID @GuildID='"+GuildID+"'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Guild guild = new Guild();
                    guild.GuildID = int.Parse(con.dataRead["GuildID"].ToString());
                    guild.GuildName = con.dataRead["GuildName"].ToString();
                    guild.Player_Number = int.Parse(con.dataRead["Player_Number"].ToString());
                    listguildid.Add(guild);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                con.CloseData();
                con = null;
            }
            return listguildid;
 
        }

    }
}